package com.fr.elastic;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Arrays;

import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;


public class ClientTest {
	private static final TransportClient client;
	static {
		try {
			Settings settings = Settings.settingsBuilder().put("cluster.name", "Master").put("client.transport.sniff", true).build();
			client = TransportClient.builder().settings(settings).build();

			client.addTransportAddresses(new InetSocketTransportAddress(InetAddress.getByName("localhost"),9200));
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
	}
	private static final AdminClient adminClient = client.admin();
	private static final ClusterAdminClient clusterAdminClient = adminClient.cluster();
	private static final IndicesAdminClient indicesAdminClient = adminClient.indices();
	
	public static void main(String[] args) {
//		CreateIndexRequest request = new CreateIndexRequest("test");
//		Settings.Builder builder = Settings.builder();
//		request.settings(builder.build());
//		indicesAdminClient.create(request);
		SearchRequestBuilder prepareSearch = client.prepareSearch("a");
		BoolQueryBuilder  b = QueryBuilders.boolQuery();
		b.should(QueryBuilders.termQuery("c", "d"));
		b.should(QueryBuilders.termQuery("f", "g"));
		prepareSearch.setQuery(b);
		prepareSearch.addAggregation(AggregationBuilders.terms("a"));
		prepareSearch.setPostFilter(b);
		System.out.println(prepareSearch.toString());
	}

}
